package com.hxd.otc.module.convenience.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.convenience.adapter.OilListAdapter;
import com.hxd.otc.module.convenience.bean.RefuelingServiceBean;
import com.hxd.otc.module.convenience.contract.OilListContract;
import com.hxd.otc.module.convenience.fragment.ConvenienceFragment;
import com.hxd.otc.module.convenience.presenter.OilListPresenter;
import com.hxd.otc.widget.TitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;

/**
 * 加油充值列表页面
 */
public class OilListActivity extends BaseActivity<OilListPresenter> implements OilListContract.View {
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @BindView(R.id.title_bar)
    TitleBar titleBar;

    OilListAdapter serviceAdapter;
    RefuelingServiceBean serviceBean;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_oil_list;
    }

    @Override
    protected void initView() {
        serviceBean = (RefuelingServiceBean) getIntent().getExtras().getSerializable(ConvenienceFragment.EXTRA_REFUELING_SERVICE_BEAN);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.setParentId(serviceBean.getId());
                mPresenter.start();
            }
        });
    }

    @Override
    protected void initData() {
        initAdapter();
        mRefreshLayout.autoRefresh(0, 10, 1);
    }

    private void initAdapter() {
        if (serviceAdapter == null) {
            serviceAdapter = new OilListAdapter();
            serviceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("info", serviceAdapter.getItem(position));
                    $startActivity(OilRechargeActivity.class, bundle);
                }
            });
        }
        recyclerView.setAdapter(serviceAdapter);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected OilListPresenter createPresenter() {
        return new OilListPresenter(this);
    }


    @Override
    public void onSetAdapter(List<RefuelingServiceBean> list) {
        serviceAdapter.setNewData(list);
    }

    @Override
    public void onShowNoMore() {
        mRefreshLayout.finishRefresh();
    }
}
